//
//  Utils.m
//  eGifr
//
//  Created by Phien Tram on 8/6/13.
//  Copyright (c) 2013 eGiftr. All rights reserved.
//

#import "Utils.h"
#import "FindKeyword.h"
#import "NSDictionary+Utility.h"

@implementation Utils

+ (int)randomValue:(int)lowerBound upperBound:(int)upperBound
{
    if (upperBound == lowerBound) return lowerBound;
    int rndValue = lowerBound + arc4random() % (upperBound - lowerBound);
    return rndValue;
}

+ (NSMutableArray *)createFlyingLabelTitles
{
    return [NSMutableArray arrayWithObjects:
            @"Museum",  //Activities
            @"Art",
            @"Chiropractor",
            @"Ballroom Classes",
            @"Wine Tasting",
            @"Exhibit",
            @"Skin Care",
            @"Hair",
            @"Tours",
            @"History",
            @"Life Skill Classes",
            @"Salsa",
            @"Theme Park",
            @"Science",
            @"Go Karting",
            @"Tango",
            @"Park",
            @"Marathon",
            @"Mini Golf",
            @"Salon",
            @"Golf",
            @"Run",
            @"RF Racing",
            @"Waxing",
            @"Bowling",
            @"Batting Cages",
            @"Spa",
            @"Hang Gliding",
            @"Skydiving",
            @"Tanning",
            @"Sewing",
            @"Rock Climbing",
            @"Skiing",
            @"Fair",
            @"Classes",
            @"Painting",
            @"Dance",
            @"Carnival",
            @"Camping",
            @"Teeth Whitening",
            @"Outdoor Adventures",
            @"Science",
            @"Dental",
            @"Running",
            @"History",
            @"Vision",
            @"Exhibit",
            @"Massage",
            @"Makeup",
            @"Eye Care",
            @"Parade",
            @"Facials",
            @"Hot Air Balloon",
            @"Halloween Events",
            @"Pageant",
            @"Nail Care",
            @"Surfing",
            @"Disney",
            @"Film Festival",
            @"Food and Drink Festival",
            @"Fundraisers",
            @"Galleries",
            @"Pets",
            @"Fashion",
            @"Charities",
            @"Launch party",
            @"Party",
            @"Cruises",
            @"Travel",
            @"Vacation",
            @"Food Trucks",
            @"CrossFit",
            @"Boot Camp",
            @"Kayak",
            @"Paddle Boarding",
            @"Flying",
            @"Kickboxing",
            @"Paintball",
            @"Horseback riding",
            @"Scrapbooking",
            @"Shaving",
            @"Photography classes",
            @"Comedy Clubs",    //Sports
            @"Skateboarding",
            @"Basketball",
            @"Motorsports",
            @"Sports Tickets",
            @"Softball",
            @"Bodybuilding",
            @"Marching Band",
            @"Baseball",
            @"Track & Field",
            @"College Sports",
            @"Lacrosse",
            @"Soccer",
            @"Track and Field",
            @"Golf",
            @"Horse Racing",
            @"Football",
            @"Tennis",
            @"Gymnastics",
            @"Hockey",
            @"Swimming",
            @"Wrestling",
            @"Figure Skating",
            @"Roller Derby",
            @"Volleyball",
            @"Fight",
            @"Rodeo",
            @"Rowing",
            @"Squash",
            @"Racing",
            @"Polo",
            @"Rugby",
            @"America’s Cup",
            @"Competition",
            @"Boxing",
            @"Mixed Martial Arts",
            @"Jazz music",  //Concers
            @"Blues music",
            @"R&B music",
            @"Rap music",
            @"Classical music",
            @"Country music",
            @"Electronica music",
            @"Rock music",
            @"Music festival",
            @"Hard rock music",
            @"Hip-hop music",
            @"Pop music",
            @"Concerts",
            @"Latin music",
            @"Performance",
            @"Orchestra",
            @"Philharmonic",
            @"Symphony",
            @"Children’s music",
            @"Folk music",
            @"Acoustic music",
            @"Broadway",    //Theater
            @"Comedy Clubs",
            @"Opera",
            @"Theater",
            @"Ballet",
            @"Cirque Du Soleil",
            @"Award Show",
            @"Musical",
            @"Play",
            @"Dance Performance Tour",
            @"Magic Show",
            @"Ice Show",
            @"Children’s Television Tour",
            @"Children’s Theater",
            @"Improv comedy",
            nil];
}

+ (NSArray *)getAllKeywords
{
    NSMutableArray *mutableArray = [NSMutableArray array];
    
    NSError *err = nil;
    NSString *dataPath = [[NSBundle mainBundle] pathForResource:@"Keywords" ofType:@"json"];
    NSArray *keywords = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:dataPath]
                                                        options:kNilOptions
                                                          error:&err];
    NSLog(@"Imported Keywords: %@", keywords);
    
    for (NSDictionary *result in keywords) {
        FindKeyword *findKeyword = [[FindKeyword alloc] init];
        findKeyword.cateName = [result objectOrNilForKey:@"cate"];
        findKeyword.subCateName = [result objectOrNilForKey:@"subCate"];
        findKeyword.keyword = [result objectOrNilForKey:@"keyword"];
        
        [mutableArray addObject:findKeyword];
    }
    
    return [NSArray arrayWithArray:mutableArray];
}

//http://stackoverflow.com/questions/1757303/parsing-json-dates-on-iphone
+ (NSDate *)mfDateFromDotNetJSONString:(NSString *)string
{
    static NSRegularExpression *dateRegEx = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        dateRegEx = [[NSRegularExpression alloc] initWithPattern:@"^\\/date\\((-?\\d++)(?:([+-])(\\d{2})(\\d{2}))?\\)\\/$" options:NSRegularExpressionCaseInsensitive error:nil];
    });
    NSTextCheckingResult *regexResult = [dateRegEx firstMatchInString:string options:0 range:NSMakeRange(0, [string length])];
    
    if (regexResult) {
        // milliseconds
        NSTimeInterval seconds = [[string substringWithRange:[regexResult rangeAtIndex:1]] doubleValue] / 1000.0;
        // timezone offset
        if ([regexResult rangeAtIndex:2].location != NSNotFound) {
            NSString *sign = [string substringWithRange:[regexResult rangeAtIndex:2]];
            // hours
            seconds += [[NSString stringWithFormat:@"%@%@", sign, [string substringWithRange:[regexResult rangeAtIndex:3]]] doubleValue] * 60.0 * 60.0;
            // minutes
            seconds += [[NSString stringWithFormat:@"%@%@", sign, [string substringWithRange:[regexResult rangeAtIndex:4]]] doubleValue] * 60.0;
        }
        
        return [NSDate dateWithTimeIntervalSince1970:seconds];
    }
    return nil;
}

@end
